/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2023-01-13
 * Time: 8:54
 */
public class SingleLinkedList {
    class Node {
        Node next;
        int value;
        public Node(int value) {
            this.value = value;
        }
    }
    public Node head;
    public void createLink() {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(2);
        Node node4 = new Node(1);
        //Node node5 = new Node(4);
        //Node node6 = new Node(5);
        //Node node7 = new Node(6);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        //node4.next = node5;
        //node5.next = node6;
        //node6.next = node7;
        this.head = node1;
    }

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if(isEmpty()) {
            head = node;
            return;
        }
        node.next = head;
        head = node;
    }

    //判断是否为空链表
    private boolean isEmpty() {
        return this.head == null;
    }

    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if(isEmpty()) {
            head = node;
            return;
        }
        Node cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        Node node = new Node(data);
        checkIndexOut(index);
        //头插
        if(index == 0) {
            addFirst(data);
            return true;
        }
        //尾插
        if(index == size()) {
            addLast(data);
            return true;
        }

        Node cur = head;
        while (index-1 != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur.next;
        cur.next = node;
        return true;
    }

    //判断输入的位置是否合法
    private void checkIndexOut(int index) {
        if(index < 0 && index > size()) {
            throw new IndexOutException("位置不合法，请检查");
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        if(isEmpty()) {
            return false;
        }
        Node cur = head;
        while(cur != null) {
            if(key == cur.value) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(isEmpty()) {
            return;
        }
        if(this.head.value == key) {
            this.head = this.head.next;
            return;
        }
        Node cur = head;
        while(cur.next != null) {
            if(cur.next.value == key) {
                cur.next.value = 0;
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(isEmpty()) {
            return;
        }
        Node cur = head;
        Node curNext = null;
        if(head != null) {
            curNext = cur.next;
        }
        while(curNext != null) {
            if(curNext.value == key) {
                cur.next = curNext.next;
                curNext = curNext.next;
            }else {
                cur = curNext;
                curNext = curNext.next;
            }
        }
        if(head.value == key){
            head = head.next;
        }
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        Node cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //打印单链表
    public void display() {
        Node cur = head;
        while(cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //清空单链表
    public void clear() {
        this.head = null;
    }
}
